//
//  XTVCOfdiantai.m
//  TTLestening
//
//  Created by dllo on 16/3/24.
//  Copyright © 2016年 dilo. All rights reserved.
//

#import "XTVCOfdiantai.h"
#import "NetworkingHandlerOfBlock.h"
#import "XTCellOfdiantaiRow.h"
#import "XTModelOfdiantai.h"
#import <SDWebImage/UIImageView+WebCache.h>
#import "XCModelForEveryone.h"


#define WIDTH  [UIScreen mainScreen].bounds.size.width
#define HEIGHT  [UIScreen mainScreen].bounds.size.height
@interface XTVCOfdiantai ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic,retain)UITableView *tableviewsection;

@property (nonatomic,retain)UITableView *tableviewrow;


@property (nonatomic,retain)NSMutableArray *arrsection;

@property (nonatomic,retain)NSMutableDictionary *dic;

@property (nonatomic,retain)NSMutableArray *arrayshuju;

@property (nonatomic, assign) NSInteger row;

@property (nonatomic,strong)NSMutableArray *songlist;


@property (nonatomic,strong)NSString *picurl;

@end

@implementation XTVCOfdiantai

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor redColor];
    self.title = @"电台";
    [self creatview];
    
    [self creatData];
}

-(void)creatData{
    
    NSString *url = @"http://api.fm.ttpod.com/radio?app=ttpod&v=v8.1.5.2016022418&user_id=0&mid=iPhone5C&f=f320&s=s310&imsi=&hid=&splus=9.2&active=1&net=2&openudid=71ae591d23e3955faffe1f13002174549b820ffe&idfa=766C509A-D4C4-4D11-89EA-A316DCF2B7CC&utdid=Vo%2FO9IPtv1UDAHSbJQCXwF%2F6&alf=201200&bundle_id=com.ttpod.music&latitude=38.88243&longtitude=121.5397";
    
    [NetworkingHandler networkingWithUrl:url completeHandle:^(NSURLResponse *response, NSData *data, id result) {
        self.arrayshuju = [NSMutableArray array];
        self.dic = [NSMutableDictionary dictionary];
        self.arrsection = [NSMutableArray array];
        NSArray *dataArr= [result objectForKey:@"data"];

        for (NSDictionary *dic in dataArr) {
            
            NSString *key = [dic objectForKey:@"tag_type_name"];

            [self.arrsection addObject:key];
            
            NSArray *arrData = [dic objectForKey:@"data"];
            NSMutableArray *arrqqqqq = [NSMutableArray array];

            for (NSDictionary *dicshuju in arrData) {
                XTModelOfdiantai *model  = [[XTModelOfdiantai alloc]init];
                
                [model setValuesForKeysWithDictionary:dicshuju];
                
                [arrqqqqq addObject:model];

               
            }
             [self.dic setObject:arrqqqqq forKey:key];
            
        }
        NSString *key  =  [self.arrsection objectAtIndex:0];
        self.arrayshuju =   [self.dic objectForKey:key];
        [self.tableviewsection reloadData];
        [self.tableviewrow reloadData];
        
        [self.tableviewsection selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:YES scrollPosition:UITableViewScrollPositionNone];
        
    }];

    
    
    
}


-(void)creatview{
 
    self.tableviewsection = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, WIDTH * 0.25 , HEIGHT - 49) style:UITableViewStylePlain];
    self.tableviewsection.backgroundColor = [UIColor lightGrayColor];
    [self.view addSubview:self.tableviewsection];
    self.tableviewsection.delegate =self;
    self.tableviewsection.dataSource = self;
    self.tableviewsection.scrollEnabled = NO;
    [self.tableviewsection registerClass:[UITableViewCell class] forCellReuseIdentifier:@"poolforsection"];
    
    self.tableviewrow = [[UITableView alloc]initWithFrame:CGRectMake(WIDTH * 0.25 - 1 , 64, WIDTH * 0.75 + 1 , HEIGHT - 49 - 64)];
    self.tableviewrow.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.tableviewrow];
    self.tableviewrow.delegate = self;
    self.tableviewrow.dataSource = self;
    self.tableviewrow.bounces = NO;
    
    UINib *nib = [UINib nibWithNibName:@"XTCellOfdiantaiRow" bundle:[NSBundle mainBundle]];
    [self.tableviewrow registerNib:nib forCellReuseIdentifier:@"poolforrow"];
    
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (tableView == self.tableviewsection) {
         return (HEIGHT - 100)/10;
    }else{
       
        return (HEIGHT - 113)/6;
    }
    
   
    
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    
    if (tableView == self.tableviewsection) {
        return self.dic.allKeys.count;
    }else{
        
     
        return self.arrayshuju.count;
    }
    
    
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (tableView == self.tableviewsection) {
        
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"poolforsection"];
        cell.backgroundColor = [UIColor lightGrayColor];
        
    cell.textLabel.text  = [self.arrsection objectAtIndex:indexPath.row];
        return cell;
    }else{
        XTCellOfdiantaiRow *cell = [tableView dequeueReusableCellWithIdentifier:@"poolforrow"];
        
    

        XTModelOfdiantai *model = [self.arrayshuju objectAtIndex:indexPath.row];
        
        cell.title.text = model.tag_name;
       
        [cell.backimage sd_setImageWithURL:[NSURL URLWithString:model.pic_url_240_200] placeholderImage:nil options:SDWebImageAvoidAutoSetImage];
        
        
        cell.count.text = [NSString stringWithFormat:@"%ld首",(long)model.quantity];
        cell.redu.text = [NSString stringWithFormat:@"视听热度 : %ld",(long)model.listen_count];
        cell.tag_id = [NSString stringWithFormat:@"%ld",(long)model.tag_id];
        
        
        return cell;
        
        
    }
    
    
}



-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (tableView == self.tableviewsection) {
        
        NSString *key  =  [self.arrsection objectAtIndex:indexPath.row];
        
        
        
        NSLog(@"%@",key);
        
        self.arrayshuju = [NSMutableArray array];
        
        self.arrayshuju =   [self.dic objectForKey:key];
   
        NSLog(@"%@",self.arrayshuju);


        
        [self.tableviewrow reloadData];
        
        
    }else{
        
        self.songlist = [NSMutableArray array];
        
       XTModelOfdiantai *model = [self.arrayshuju objectAtIndex:indexPath.row];
        
        NSString *urlstr = [NSString stringWithFormat:@"http://api.fm.ttpod.com/radiosong?num=150&tagid=%ld&app=ttpod&v=v8.1.5.2016022418",model.tag_id];
        
        
        [NetworkingHandler networkingNOSaveWithUrl:urlstr completeHandle:^(NSURLResponse *response, NSData *data, id result) {
            
            NSArray *arr = [result objectForKey:@"data"];
            
            for (NSDictionary *dic in arr) {
                
              XCModelForEveryone   *model = [[XCModelForEveryone alloc] init];
                
                [model setValuesForKeysWithDictionary:dic];
                
                model.urlList = [dic objectForKey:@"url_list"];
                model.name = [dic objectForKey:@"song_name"];
                model.mvList = [dic objectForKey:@"mv_list"];
                model.songId = [dic objectForKey:@"song_id"];
                
                model.duration = [[model.urlList objectAtIndex:0]objectForKey:@"duration"];
                
                model.singerName = [dic objectForKey:@"singer_name"];
                
                
         //百度图片
//                NSString *picstr = [NSString stringWithFormat:@"http://search.dongting.com/artwork/search?singerid=0&artist=%@&title=&x=640&y=1136&app=ttpod&v=v8.1.5.2016022418&user_id=407738183&mid=iPhone5C&f=f320&s=s310&imsi=&hid=&splus=9.3.1&active=1&net=2&openudid=71ae591d23e3955faffe1f13002174549b820ffe&idfa=766C509A-D4C4-4D11-89EA-A316DCF2B7CC&utdid=Vo%2FO9IPtv1UDAHSbJQCXwF%2F6&alf=201200&bundle_id=com.ttpod.music&latitude=38.88262&longtitude=121.5397&access_token=6fa1fd534a1a5594dea73639eaf7b725",model.singerName];
//                
//               
//                
//                [NetworkingHandler networkingNOSaveWithUrl:picstr completeHandle:^(NSURLResponse *response, NSData *data, id result) {
//                   
//                    NSArray *arrwe = [result objectForKey:@"data"];
//                    
//                    NSDictionary *dic = [arrwe objectAtIndex:0];
//                
//                    
//                    NSArray *arrat = [dic objectForKey:@"picUrls"];
//                    
//                    NSDictionary *dic2 = [arrat objectAtIndex:0];
//                    
//                    model.picUrl = [dic2 objectForKey:@"picUrl"];
                
                     
//                }];
    
//                NSLog(@"%@",model.picUrl);
               
                [self.songlist addObject:model];
            }
            
             NSLog(@"%ld",self.songlist.count);
            
            [[NSNotificationCenter defaultCenter] postNotificationName:@"giveValue" object:nil userInfo:@{@"modelArray": self.songlist, @"index": @0}];
            
            
            
        }];
        
        
        
        
    }
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
